import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface XYCoord {
  x: number;
  y: number;
}

//存放柜当前正在拖放的独一份数据

export interface OperationState {
  //drag
  dragSouce?: { id: number; text: string };
  dragParentName?: 'left' | 'right';
  dragId?: string;
  dragIndex?: number;
  dragStartNodeClientOffset?: XYCoord | null;
  dragStartEventClientOffset?: XYCoord | null;
  //drop
  dropId?: string;
  dropIndex?: number;
  dropParentName?: 'left' | 'right';
  dropEventClientOffset?: XYCoord | null;
  dropNodeClientOffset?: XYCoord | null;
  dropResult?: any;
  isSourcePublic?: boolean;
}

const dragOperationSlice = createSlice({
  name: 'dragOperationStore',
  initialState: () => ({}),
  reducers: {
    beginDrag: (state, action: PayloadAction<OperationState>) => {
      return action.payload;
    },
    publishDragSource: (state, action: PayloadAction<OperationState>) => {
      return {
        ...state,
        isSourcePublic: true,
      };
    },
    over: (state, action: PayloadAction<OperationState>) => {
      return {
        ...state,
        ...action.payload,
      };
    },
    updateItem: (state, action: PayloadAction<OperationState>) => {
      const newState = {
        ...state,
        item: action.payload,
      };
      return newState;
    },
    drop: (state, action: PayloadAction<OperationState>) => {
      return {
        ...state,
        ...action.payload,
      };
    },
    endDrag: (state, action: PayloadAction<OperationState>) => {
      return action.payload;
    },
  },
});

export default dragOperationSlice;
